package org.jeecg.modules.miniAppMerchantEndPoint.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.miniAppCustomerEndPoint.utils.JxUserUtil;
import org.jeecg.modules.miniAppMerchantEndPoint.mapper.JxStoreOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/jx/merchant_order")
public class JxStoreOrderController {

    @Autowired
    private JxStoreOrderMapper jxStoreOrderMapper;

    /**
     * 接口1.2 查看当天该店铺的订单统计，菜品名，份数
     * @param request
     * @param storeIdForDebug
     * @return
     */
    @ApiOperation("获取商户端订单页面的统计信息")
    @GetMapping("/getOrderStatistics")
    public Result<?> getOrderStatistics(
            HttpServletRequest request,
            @RequestParam(value = "storeId", required = false) String storeIdForDebug) {

        String token = request.getHeader("X-Access-Token");
        String storeId = JxUserUtil.getUserId(token, storeIdForDebug);

        JSONObject resultData = new JSONObject();
        // 订单统计时间 及 公告
        resultData.put("orderStatisticsTimeBegin", "19:00");
        resultData.put("orderStatisticsTimeEnd", "23:00");
        resultData.put("merchantAnnouncement", "投诉多的店会限购,xxxxxx");
        List<HashMap<String,Object>> list = jxStoreOrderMapper.countPayedStoreOrder(storeId);
        // 获取统计单量列表, 返回 foodId, foodName, selectedSpecification, sum(count) from jx_order_detail;
        resultData.put("hcOrderDetailedList",list);

        return Result.ok(list);
    }


    /**
     * 接口1.3 查看当天该店铺的订单统计详情
     * @param request
     * @param storeIdForDebug
     * @return
     */
    @ApiOperation("获取商户端订单页面的统计信息")
    @GetMapping("/getStoreOrderDetail")
    public Result<?> getStoreOrderDetail(
            HttpServletRequest request,
            @RequestParam(value = "storeId", required = false) String storeIdForDebug,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        String token = request.getHeader("X-Access-Token");
        String storeId = JxUserUtil.getUserId(token, storeIdForDebug);

        List<HashMap<String,Object>> list = jxStoreOrderMapper.queryPayedStoreOrderDetail(storeId);
        return Result.ok(list);
    }

    /**
     * 接口1.4 更新店铺 出餐状态，有如下几种状态
     * 未开始出餐
     * 出餐中
     * 已完成出餐（已打菜骑手未取餐）
     * 已配送（骑手已取餐）
     * @param request
     * @param storeIdForDebug
     * @return
     */
    @ApiOperation("获取商户端订单页面的统计信息")
    @GetMapping("/updateStoreBusinessState")
    public Result<?> updateStoreBusinessState(
            HttpServletRequest request,
            @RequestParam(value = "storeId", required = false) String storeIdForDebug,
            @RequestParam(name = "state") String state) {

        return Result.ok();
    }

    /**
     * 接口1.5 将订单数据导出到表格，并返回表格文件的url
     * @param request
     * @param storeIdForDebug
     * @return
     */
    @ApiOperation("导出订单数据到表格")
    @GetMapping("/exportOrder")
    public Result<?> exportOrder(
            HttpServletRequest request,
            @RequestParam(value = "storeId", required = false) String storeIdForDebug) {

        // todo 导出到excel，并返回可下载的url

        return Result.ok();
    }


}
